/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crp.object;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Hashtable;

/**
 *
 * @author Kresnik
 */
public class Article {
    
    private static ArrayList<File> articleList = new ArrayList<File>();
    
    public static void addArticle(File[] filelist){
        for(File f : filelist)
            if(!f.isDirectory() && !articleList.contains(f)){
                String fname = f.getName();
                int dotIndex = fname.lastIndexOf(".");
                if( fname.substring(dotIndex).equals(".txt")){
                    Article.articleList.add(f);
                }
            }
            else if(f.isDirectory()){
                String[] flist = f.list();
                File[] ftemp = new File[flist.length];
                String abspath = f.getAbsolutePath()+"/";
                for(int i = 0 ; i < flist.length ; i++){
                    ftemp[i] = new File(abspath+flist[i]);
                }
                
                Article.addArticle(ftemp);
            }
    }
    
    public static void addArticle(File f){
    	if(!f.isDirectory() && !articleList.contains(f)){
    		String fname = f.getName();
            int dotIndex = fname.lastIndexOf(".");
            if( fname.substring(dotIndex).equals(".txt")){
                Article.articleList.add(f);
            }
    	}else if(f.isDirectory()){
            String[] flist = f.list();
            File[] ftemp = new File[flist.length];
            String abspath = f.getAbsolutePath()+"/";
            for(int i = 0 ; i < flist.length ; i++){
                ftemp[i] = new File(abspath+flist[i]);
            }
            
            Article.addArticle(ftemp);
        }
    }
    
    public static void deleteArticle(File[] filelist){
        for(File f : filelist)
            if(!f.isDirectory()){
                if(Article.articleList.contains(f))
                    Article.articleList.remove(f);
            }
            else{
                String[] flist = f.list();
                File[] ftemp = new File[flist.length];
                for(int i = 0 ; i < flist.length ; i++){
                    ftemp[i] = new File(flist[i]);
                }
                Article.deleteArticle(ftemp);
            }
    }
    
    public static void deleteArticle(int index){
        Article.articleList.remove(index);
    }
    
    public static void deleteArticle(int[] indices){
        for(int i : indices)
            articleList.remove(i);
    }
    
    public static void clearArticle(){
        Article.articleList.clear();
    }
    
    public static ArrayList<File> getArticleList(){
        return Article.articleList;
    }
}
